package com.sinosoft.ptas.control;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.sinosoft.framework.core.common.exception.BusinessException;
import com.sinosoft.framework.core.common.utils.StringUtil;
import com.sinosoft.framework.core.common.utils.ajax.ResponseData;
import com.sinosoft.ptas.control.dto.AuditData;
import com.sinosoft.ptas.entity.AuditEntity;
import com.sinosoft.ptas.service.AuditService;
import com.sinosoft.ptas.service.AuditUpdateService;
import com.sinosoft.ptas.service.ReportService;
import com.sinosoft.ptas.system.commom.AuditConstant;
import com.sinosoft.ptas.system.commom.ExceptionConstant;

/**
 * 审核表 控制层
 * @author zhangxiaolong
 *
 */
@Controller
@RequestMapping(value="audit")
public class AuditController {
	@Resource
	AuditService auditService;
	
	@Resource
	ReportService reportService;
	
	@Resource
	AuditUpdateService auditUpdateService;

	/**
	 * 查询全部审核表
	 * @return 审核表集合 auditDataList
	 */
	@RequestMapping(value="/retrieveAudit.service",method=RequestMethod.POST)
	@ResponseBody
	public ResponseData retrieveAudit(){
		List<AuditEntity> auditList=new ArrayList<AuditEntity>();
		AuditEntity auditEntity=new AuditEntity();
		auditList=auditService.retrieveAudit(auditEntity);
		List<AuditData> auditDataList=toAuditDatas(auditList);
		if(auditDataList==null){
			return new ResponseData(false,ExceptionConstant.DATA_NULL_MESSAGE);
		}else{
			return new ResponseData(true,auditDataList);
		}
	} 
	
	/**
	 * 根据reportid查询审核表
	 * @return 审核表  auditData
	 */
	@RequestMapping(value="/retrieveAuditByReportId.service",method=RequestMethod.POST)
	@ResponseBody
	public ResponseData retrieveAuditByReportId(String reportId){
		AuditEntity auditEntity=new AuditEntity();
		auditEntity=auditService.retrieveAuditByReportId(reportId);
		AuditData auditData=toauditData(auditEntity);
		if(auditData==null){
			return new ResponseData(false,ExceptionConstant.DATA_NULL_MESSAGE);
		}else{
			return new ResponseData(true,auditData);
		}
	} 
	
	
	/**
	 * 保存操作
	 * 先判断保存的数据是不是第一次，是第一次就添加，不是就更新
	 * @return 审核表传输类
	 */
	@RequestMapping(value="/auditSave.service",method=RequestMethod.POST)
	@ResponseBody
	public ResponseData auditSave(AuditData auditData){
		
		if(auditData==null){
			return new ResponseData(false,ExceptionConstant.DATA_NULL_CODE,ExceptionConstant.DATA_NULL_MESSAGE,null);
		}
		try{
		if(auditData.getStateOFAuditTable().equals(AuditConstant.AUDIT_SAVE)){
			AuditEntity auditEntity=auditData.toEntity();//传输类转为实体类
			AuditEntity audit= auditService.retrieveAuditByReportId(auditEntity.getReportId());//依据申报标的id查出审核表
			if(audit!=null){
				if(audit.getStateOFAuditTable().equals(AuditConstant.AUDIT_SAVE)){
					auditService.updateAudit(auditEntity); 
				}else
					return new ResponseData(false,ExceptionConstant.AUDIT_ONLY_ONE_CODE,ExceptionConstant.AUDIT_ONLY_ONE_NAME,null);
			}else{
			String id="";
			if(StringUtil.isBlank(auditData.getId())){//判断数据库里有没有该条数据
				id=auditService.createAudit(auditEntity);
				auditData.setId(id);
			}/*else{
				if(StringUtil.isBlank(auditEntity.getId())){
					return new ResponseData(false,ExceptionConstant.ID_NULL_CODE,ExceptionConstant.ID_NULL_MESSAGE,null);
				}else{
					auditService.updateAudit(auditEntity); 
				}
			}*/
		}	
			return new ResponseData(true,auditData);
		}else{
			
			AuditEntity auditEntity=new AuditEntity();
			auditEntity=auditData.toEntity();
			AuditEntity audit= auditService.retrieveAuditByReportId(auditEntity.getReportId());//依据申报标的id查出审核表
			if(audit!=null){//判断数据库中是否有值
			if(audit.getStateOFAuditTable().equalsIgnoreCase(AuditConstant.AUDIT_COMMIT)){//如果状态是提交状态，就不能进行二次提交
				return new ResponseData(false,ExceptionConstant.AUDIT_ONLY_ONE_CODE,ExceptionConstant.AUDIT_ONLY_ONE_NAME,null);
				
			}else{
				auditUpdateService.AuditUpdateAffair(auditEntity);
		   }
	   }else{
		   auditUpdateService.AuditUpdateAffair(auditEntity);
	   }
			return new ResponseData(true,auditData);
		}
		}catch(BusinessException e){
			return new ResponseData(false,e.getErrorCode(),e.getMessage(),null);
		}
		
	}
	
	/**
	 * 提交操作(暂时不用，已经合并到保存操作中)
	 * 由于提交操作必须在保存后，所以不需要创建id 直接更新就可以，
	 * 同时将申报表中的 项目状态以及评定等级进行相应的修改
	 * 返回的信息自带id值
	 * @return 审核表传输类
	 */
	@RequestMapping(value="/auditCommit.service",method=RequestMethod.POST)
	@ResponseBody
	public ResponseData auditSubmit(@RequestBody AuditData auditData){
		if(auditData==null){
			return new ResponseData(false,ExceptionConstant.DATA_NULL_MESSAGE);
		}
		
		if(StringUtil.isBlank(auditData.getId())){//判断数据库里有没有该条数据
				auditSave(auditData);
				
		}
			AuditEntity auditEntity=new AuditEntity();
			auditEntity=auditData.toEntity();
			auditUpdateService.AuditUpdateAffair(auditEntity);
			return new ResponseData(true,auditData);
		
		
	}
	
	
	
	/**
	 * 添加审核表  
	 * @param auditData
	 * @return 带有UUID的审核表传输类或者false
	 */
	@RequestMapping(value="/createAudit.service",method=RequestMethod.POST)
	@ResponseBody
	public ResponseData createAudit(@RequestBody AuditData auditData){
		if(auditData==null){
			return new ResponseData(false,ExceptionConstant.DATA_NULL_MESSAGE);
		}
		try{
		auditData.setId(UUID.randomUUID().toString());
		AuditEntity auditEntity=auditData.toEntity();
		String id=auditService.createAudit(auditEntity);
		if(id!=null)
		{
			return new ResponseData(true,auditData);
		}else{
			return new ResponseData(false,null);
		}
		}catch(BusinessException e){
			return new ResponseData(false,e.getErrorCode(),e.getMessage(),null);
		}
	}
	
	
	/**
	 * 审核表实体类集合转换为传输类集合 
	 * @param 
	 * @return 
	 */
	private List<AuditData> toAuditDatas(List<AuditEntity> auditEntitys) {
		int size = auditEntitys.size();
		List<AuditData> auditDatas = new ArrayList<AuditData>();
		for (int i = 0; i < size; i++) { // 循环将实体数据转换为传输数据
			AuditData auditData = new AuditData();
			auditData.toauditData(auditEntitys.get(i));
			auditDatas.add(auditData);
		}
		return auditDatas;
	}

	/**
	 * 审核表实体类转为传输类
	 * @param 
	 * @return 审核表传输类
	 */
	public AuditData toauditData(AuditEntity auditEntity) {
		AuditData auditData = new AuditData();
		auditData.toauditData(auditEntity);
		return auditData;
	}
	
	
}
